<?php
/**
 * Created by PhpStorm.
 * User: j
 * Date: 2019-01-03
 * Time: 15:55
 */
namespace common\services\tool;

class ToolBankCard
{
    /**
     * Luhn算法校验银行卡号
     * @param $bankCard
     * @return bool
     */
    public static function verifyBankCard($bankCard)
    {
        $arrNo = str_split($bankCard);
        $lastN = $arrNo[count($arrNo) - 1];
        krsort($arrNo);
        $i = 1;
        $total = 0;
        foreach ($arrNo as $n) {
            if ($i % 2 == 0) {
                $ix = $n * 2;
                if ($ix >= 10) {
                    $nx = 1 + ($ix % 10);
                    $total += $nx;
                } else {
                    $total += $ix;
                }
            } else {
                $total += $n;
            }
            $i++;
        }
        $total -= $lastN;
        $total *= 9;
        if ($lastN == ($total % 10)) {
            return true;
        }
        return false;
    }
}